home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / ugraph2.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  5.8 KB  |  208 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  ugraph2.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_UGRAPH_H
  16. #define LEDA_UGRAPH_H
  17.  
  18. #include <LEDA/graph.h>
  19.  
  20.  
  21. //-----------------------------------------------------------------------------
  22. // ugraph: base class for all undirected graphs
  23. //-----------------------------------------------------------------------------
  24.  
  25. class ugraph : public graph{
  26.  
  27. private:
  28.  
  29. edge sym_edge(edge e) const { return e->rev; }
  30.  
  31.  
  32. public:
  33.  
  34.  
  35. edge new_edge(node v,node w,GenPtr i);
  36. edge new_edge(node v,node w,edge e1,edge e2,GenPtr i,int d1,int d2) ;
  37.  
  38. edge new_edge(node v,node w)
  39. { GenPtr x; init_edge_entry(x);
  40.   return new_edge(v,w,x);
  41.  }
  42.  
  43. edge new_edge(node v, node w, edge e1 ,edge e2, int dir1=0,int dir2=0)
  44. { GenPtr x; init_edge_entry(x);
  45.   return new_edge(v,w,e1,e2,x,dir1,dir2);
  46.  }
  47.  
  48. void del_edge(edge);
  49.  
  50. void rev_edge(edge) {}
  51.  
  52. node opposite(node v, edge e)  const { return (v==e->s) ? e->t : e->s; }
  53.  
  54. list<node> adj_nodes(node) const;
  55.  
  56. edge adj_succ(edge e,node v)  const
  57. { if (v==e->t) e = sym_edge(e);
  58.   return graph::adj_succ(e);
  59.  }
  60.  
  61. edge adj_pred(edge e,node v)  const
  62. { if (v==e->t) e = sym_edge(e);
  63.   return graph::adj_pred(e);
  64.  }
  65.  
  66. edge cyclic_adj_succ(edge e,node v)  const
  67. { if (v==e->t) e = sym_edge(e);
  68.   return graph::cyclic_adj_succ(e);
  69.  }
  70.  
  71. edge cyclic_adj_pred(edge e,node v)  const
  72. { if (v==e->t) e = sym_edge(e);
  73.   return graph::cyclic_adj_pred(e);
  74.  }
  75.  
  76.  
  77. void print_edge(edge, ostream& = cout) const;
  78.  
  79. void read(string s)    { graph::read(s);  make_undirected(); } 
  80. void read(istream& in) { graph::read(in); make_undirected(); } 
  81.  
  82. ugraph()  { undirected = true; }
  83.  
  84. ugraph(const graph& a): graph(a) { undirected = true;  make_undirected(); }
  85.  
  86. ugraph(const ugraph& a) : graph(a)  { undirected = true;  }
  87.  
  88. ~ugraph() { /* ~graph does the job */ }
  89.  
  90. //subgraph constructors
  91. ugraph(ugraph&, const list<node>&, const list<edge>&);
  92. ugraph(ugraph&, const list<edge>&);
  93.  
  94. ugraph& operator=(const ugraph& a) { graph::operator=(a); return *this; }
  95.  
  96. ugraph& operator=(const graph& a)  { graph::operator=(a); 
  97.                                      make_undirected(); 
  98.                                      return *this;
  99.                                     }
  100.  
  101. };
  102.  
  103.  
  104.  
  105. //------------------------------------------------------------------------------
  106. // UGRAPH: generic ugraphs
  107. //------------------------------------------------------------------------------
  108.  
  109.  
  110. template<class vtype, class etype>
  111.  
  112. class _CLASSTYPE UGRAPH : public ugraph {
  113.  
  114. vtype X;
  115. etype Y;
  116.  
  117. void copy_node_entry(GenPtr& x) const { x=Copy(ACCESS(vtype,x)); }
  118. void copy_edge_entry(GenPtr& x) const { x=Copy(ACCESS(etype,x)); }
  119.  
  120. void clear_node_entry(GenPtr& x) const { Clear(ACCESS(vtype,x)); }
  121. void clear_edge_entry(GenPtr& x) const { Clear(ACCESS(etype,x)); }
  122.  
  123. void write_node_entry(ostream& o, GenPtr& x) const{ Print(ACCESS(vtype,x),o);}
  124. void write_edge_entry(ostream& o, GenPtr& x) const{ Print(ACCESS(etype,x),o);}
  125.  
  126. void read_node_entry(istream& i, GenPtr& x) { Read(X,i); x=Copy(X); }
  127. void read_edge_entry(istream& i, GenPtr& x) { Read(Y,i); x=Copy(Y); }
  128.  
  129. void init_node_entry(GenPtr& x) { Init(X); x = Copy(X); }
  130. void init_edge_entry(GenPtr& x) { Init(Y); x = Copy(Y); }
  131.  
  132. void print_node_entry(ostream& o, GenPtr& x)  const
  133.      { o << "("; Print(ACCESS(vtype,x),o); o << ")"; }
  134. void print_edge_entry(ostream& o, GenPtr& x)  const
  135.      { o << "("; Print(ACCESS(etype,x),o); o << ")"; }
  136.  
  137. char* node_type()  const { return TYPE_NAME(vtype); }
  138. char* edge_type()  const { return TYPE_NAME(etype); }
  139.  
  140. public:
  141.  
  142. int cmp_node_entry(node x, node y) const { return compare(inf(x),inf(y)); }
  143. int cmp_edge_entry(edge x, edge y) const { return compare(inf(x),inf(y)); }
  144.  
  145. vtype  inf(node v)         const { return ACCESS(vtype,ugraph::inf(v)); }
  146. etype  inf(edge e)         const { return ACCESS(etype,ugraph::inf(e)); }
  147. vtype& operator[] (node v)       { return ACCESS(vtype,entry(v)); }
  148. vtype  operator[] (node v) const { return ACCESS(vtype,ugraph::inf(v)); }
  149. etype& operator[] (edge e)       { return ACCESS(etype,entry(e)); }
  150. etype  operator[] (edge e) const { return ACCESS(etype,ugraph::inf(e)); }
  151. void   assign(node v,vtype x)    { graph::assign(v,Convert(x)); }
  152. void   assign(edge e,etype x)    { graph::assign(e,Convert(x)); }
  153.  
  154. node   new_node()        { return ugraph::new_node(); }
  155. node   new_node(vtype a) { return graph::new_node(Copy(a)); }
  156.  
  157. edge   new_edge(node v, node w) 
  158.                    { return ugraph::new_edge(v,w); }
  159.  
  160. edge   new_edge(node v, node w, etype a) 
  161.                    { return ugraph::new_edge(v,w,Copy(a)); }
  162. edge   new_edge(node v,node w,edge e1,edge e2,etype a,int dir1=0,int dir2=0)
  163.                    { return ugraph::new_edge(v,w,e1,e2,Copy(a),dir1,dir2); }
  164.  
  165. void   clear()     { clear_all_entries(); ugraph::clear(); }
  166.  
  167. UGRAPH<vtype,etype>& operator=(const UGRAPH<vtype,etype>& a)
  168. { clear_all_entries();
  169.   ugraph::operator=(*(ugraph*)&a);
  170.   copy_all_entries();
  171.   return *this;}
  172.  
  173. UGRAPH<vtype,etype>& operator=(const graph& a)
  174. { clear_all_entries();
  175.   ugraph::operator=(a);
  176.   copy_all_entries();
  177.   return *this;
  178.  }
  179.  
  180. UGRAPH() {}
  181.  
  182. UGRAPH(const UGRAPH<vtype,etype>& a): ugraph(*(ugraph*)&a) 
  183. { copy_all_entries(); }
  184.  
  185. UGRAPH(const graph& a) : ugraph(a)            
  186. { copy_all_entries(); }
  187.  
  188. ~UGRAPH() 
  189. { if (parent==0) clear_all_entries(); }
  190.  
  191. };
  192.  
  193.  
  194.  
  195. extern void complete_ugraph(ugraph&,int);
  196. extern void random_ugraph(ugraph&,int,int);
  197. extern void test_ugraph(ugraph&);
  198.  
  199. #ifndef __ZTC__
  200. inline void complete_graph(ugraph& U,int n)     { complete_ugraph(U,n); }
  201. inline void random_graph(ugraph& U,int n,int m) { random_ugraph(U,n,m); }
  202. inline void test_graph(ugraph& U)               { test_ugraph(U); }
  203. #endif
  204.  
  205.  
  206. #endif
  207.